/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.shiro.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.apache.shiro.util.CollectionUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * @author Arisgo
 * @since 2024/1/29
 */
public class JWTUtil {

    /**
     * jwt加签
     *
     * @param secret     秘钥
     * @param expireTime 过期时间
     * @param payloads   加签内容
     * @return token
     */
//    public static String sign(String secret, int expireTime, Map<String, Object> payloads) {
//        Calendar instance = Calendar.getInstance();
//        instance.add(Calendar.HOUR, expireTime);
//
//        JWTCreator.Builder jwtBuilder = JWT.create().withExpiresAt(instance.getTime());
//        for (Map.Entry<String, Object> entry : payloads.entrySet()) {
//            Object value = entry.getValue();
//            if (value instanceof Boolean) {
//                jwtBuilder.withClaim(entry.getKey(), (Boolean) value);
//            } else if (value instanceof String) {
//                jwtBuilder.withClaim(entry.getKey(), value.toString());
//            } else if (value instanceof Integer) {
//                jwtBuilder.withClaim(entry.getKey(), (Integer) value);
//            } else if (value instanceof Long) {
//                jwtBuilder.withClaim(entry.getKey(), (Long) value);
//            } else if (value instanceof Double) {
//                jwtBuilder.withClaim(entry.getKey(), (Double) value);
//            } else if (value instanceof Date) {
//                jwtBuilder.withClaim(entry.getKey(), (Date) value);
//            } else if (value instanceof Instant) {
//                jwtBuilder.withClaim(entry.getKey(), (Instant) value);
//            } else if (value instanceof List<?>) {
//                jwtBuilder.withClaim(entry.getKey(), (List<?>) value);
//            }
//        }
//        return jwtBuilder.sign(Algorithm.HMAC256(secret));
//    }

    public static String sign(String secret, int expireTime, Map<String, String> payloads) {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.HOUR, expireTime);

        JWTCreator.Builder jwtBuilder = JWT.create().withExpiresAt(instance.getTime());
        if (!CollectionUtils.isEmpty(payloads)) {
            payloads.forEach(jwtBuilder::withClaim);
        }
        return jwtBuilder.sign(Algorithm.HMAC256(secret));
    }

    /**
     * 获取指定内容
     *
     * @param token token
     * @param key   获取内容的key
     * @return 值
     */
    public static Claim getClaim(String token, String key) {
        DecodedJWT jwt = JWT.decode(token);
        return jwt.getClaim(key);
    }

    /**
     * 验证签名是否有效，是否过期
     *
     * @param token  token
     * @param secret 秘钥
     * @return true/false
     */
    public static boolean verify(String token, String secret) {
        try {
            // 根据密码生成JWT效验器
            DecodedJWT verify = JWT.require(Algorithm.HMAC256(secret)).build().verify(token);
            // 效验TOKEN有效性
            return new Date().compareTo(verify.getExpiresAt()) < 1;
        } catch (Exception e) {
            return false;
        }
    }

}
